home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #11 / Amiga Plus CD - 2002 - No. 11.iso / Tools / Development / TinyGL / ami / content / ad709 / tinygl / examples / c / blend.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-08-15  |  3.3 KB  |  135 lines

  1. /*******************************************************************
  2. *    Author: Kevin Hawkins
  3. *    Description: Displays two rotating polygons, with one
  4. *                 of the polygons having transparency (alpha = 0.6).
  5. *                 Pressing the spacebar will switch which polygon is
  6. *                 transparent.
  7. ********************************************************************/
  8.  
  9. #include <ad709/tinygl/glut.h>
  10.  
  11. ////// Global Variables
  12. float angle = 0.0f;                        // current angle of the rotating triangle
  13. GLboolean fullScreen = GL_FALSE;                    // true = fullscreen; false = windowed
  14. GLboolean keyPressed[256];                    // holds true for keys that are pressed
  15.  
  16. GLboolean leftFirst = GL_TRUE;                    // true = draw left poly first; false = right first
  17.  
  18. // Initialize
  19. // desc: initializes OpenGL
  20. void Initialize()
  21. {
  22.     glEnable(GL_BLEND);                    // enable blending
  23.  
  24.     // transparency
  25.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  26.  
  27.     glShadeModel(GL_SMOOTH);            // use smooth shading
  28.  
  29.     // Clear background to black
  30.     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  31. }
  32.  
  33. // DrawLeftPoly()
  34. // desc: draws the left polygon
  35. void DrawLeftPoly()
  36. {
  37.     glBegin(GL_QUADS);
  38.         glColor4f(0.8f, 0.9f, 0.7f, 0.6f);
  39.         glVertex3f(-10.0f, -10.0f, 0.0f);
  40.         glColor4f(0.8f, 0.9f, 0.7f, 0.6f);
  41.         glVertex3f(0.0f, -10.0f, 0.0f);
  42.         glColor4f(1, 1, 1, 0.6);
  43.         glVertex3f(0.0f, 10.0f, 0.0f);
  44.         glColor4f(1, 1, 1, 0.6);
  45.         glVertex3f(-10.0f, 10.0f, 0.0f);
  46.     glEnd();
  47. }
  48.  
  49. // DrawRightPoly()
  50. // desc: draws the right polygon
  51. void DrawRightPoly()
  52. {
  53.     glBegin(GL_QUADS);
  54.         glColor4f(0.0f, 0.5f, 0.5f, 0.6f);
  55.         glVertex3f(0.0f, -10.0f, 0.0f);
  56.         glColor4f(0.0f, 0.5f, 0.5f, 0.6f);
  57.         glVertex3f(10.0f, -10.0f, 0.0f);
  58.         glColor4f(1, 1, 1, 0.6);
  59.         glVertex3f(10.0f, 10.0f, 0.0f);
  60.         glColor4f(1, 1, 1, 0.6);
  61.         glVertex3f(0.0f, 10.0f, 0.0f);
  62.     glEnd();
  63. }
  64.  
  65.  
  66. // Render
  67. // desc: handles drawing of scene
  68. void Render()
  69. {
  70.     // clear screen and depth buffer
  71.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);                                        
  72.     glLoadIdentity();
  73.  
  74.     if (angle >= 359.9f)
  75.         angle = 0.0f;
  76.  
  77.     angle += 0.1;            // increase rotation angle
  78.  
  79.     glTranslatef(0.0f, 0.0f, -40.0f);
  80.  
  81.     // rotate and draw the opaque polygon
  82.     glPushMatrix();
  83.         glRotatef(angle, 0.0f, 0.0f, 1.0f);
  84.         if (leftFirst)
  85.             DrawLeftPoly();
  86.         else
  87.             DrawRightPoly();
  88.     glPopMatrix();
  89.  
  90.     // rotate and draw the transparent polygon
  91.     glPushMatrix();
  92.         glRotatef(angle, 0.0f, 0.0f, -1.0f);
  93.         if (leftFirst)
  94.             DrawRightPoly();
  95.         else
  96.             DrawLeftPoly();
  97.     glPopMatrix();
  98.  
  99.     glFlush();
  100. }
  101.  
  102.  
  103. void resize(int width, int height) {
  104.     glViewport(0, 0, width, height);    // reset the viewport to new dimensions
  105.     glMatrixMode(GL_PROJECTION);        // set projection matrix current matrix
  106.     glLoadIdentity();                    // reset projection matrix
  107.  
  108.     // calculate aspect ratio of window
  109.     gluPerspective(54.0f,(GLfloat)width/(GLfloat)height,1.0f,1000.0f);
  110.  
  111.     glMatrixMode(GL_MODELVIEW);            // set modelview matrix
  112.     glLoadIdentity();                    // reset modelview matrix
  113. }
  114.  
  115.  
  116. void idle() {
  117.     glutPostRedisplay();
  118. }
  119.  
  120.  
  121. int main (int argc, char **argv) {
  122.     int glutWindow;
  123.     glutInit(&argc, argv);
  124.     glutInitWindowSize(320, 240);
  125.     glutInitWindowPosition(0, 0);
  126.     glutWindow = glutCreateWindow("OpenGL blend");
  127.     Initialize();
  128.     glutDisplayFunc(Render);
  129.     glutReshapeFunc(resize);
  130.     glutIdleFunc(idle);
  131.     glutMainLoop();
  132.  
  133.  
  134.     return 0;
  135. }